<?php if (!defined("_ECRIRE_INC_VERSION")) return;
/**
 * Aloha Cart
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage alohacart
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/AlohaCart
 */

/**
 *  Cart form
 */ 

include_spip('classes/alohacart');

function formulaires_alohacart_cart_charger_dist(){
	$valeurs = array();
	$cart = new AlohaCart();
	$valeurs["cart_products"] = $cart->GetProducts();
    $GLOBALS['alohacart_cart_products'] = $cart->GetProducts(); 
	return $valeurs;
}

function formulaires_alohacart_cart_verifier_dist(){
	$erreurs = array();
    
	return $erreurs;
}

function formulaires_alohacart_cart_traiter_dist(){
    if (!_request('url')) {
    $cart = new AlohaCart();
    
    // Refresh products:
    if (_request('refresh_submit')) {
        $refresh_products = _request("products");
        $cart_products = $cart->GetProducts();
        // Refresh quantity and price
        foreach ($refresh_products as $id_product=>$product) {
            $p = $cart_products[$id_product];
            $p['quantity'] = intval($product['quantity']); 
            $p['price'] = $product['quantity']*$p['price_one'];
            if ($p['quantity'] > 0) 
                $cart->UpdateProduct($id_product, $p);
            else 
                $cart->RemoveProduct($id_product);    
        }
        
        // Delete product:
        $id_product = _request('delete_product');
        if ($id_product)
            $cart->RemoveProduct($id_product);
        
        
        $message_ok = _T('alohacart:products_refreshed');
    }
    
    // Clear Cart
    if (_request('clear_submit')) {
        $cart->ClearCart(); 
    }
    }
    return array('message_ok'=>$message_ok);    
}

?>